package com.benjaminwan.read.free.config

import com.benjaminwan.read.free.base.viewmodel.state
import com.benjaminwan.read.free.model.ItemConfig
import com.benjaminwan.read.free.model.enums.ItemType
import com.benjaminwan.read.free.model.listOfItemConfig
import com.benjaminwan.read.free.ui.main.MainViewModel
import com.benjaminwan.read.free.utils.*
import com.xuexiang.xui.widget.textview.supertextview.SuperTextView

object GroupConfig {
    fun getGroupConfig(
        mainVM: MainViewModel, idGroup: Int
    ): List<ItemConfig> {
        return listOfItemConfig {
            itemConfig {
                type = ItemType.Text
                titleStr = "备注"
                rightTextFunc = { mainVM.state.script.groups[idGroup].memo }
                click = { view ->
                    (view as SuperTextView).context.showTextInputDialog(
                        title, "请输入$title", rightText, "", 0, -1, null, "默认",
                        { dialog, which ->
                            val default = "组${idGroup + 1}"
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val group = mainVM.state.script.groups[idGroup]
                        mainVM.setGroup(idGroup, group.copy(memo = input))
                        dialog.dismiss()
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "循环"
                rightTextFunc = { "${mainVM.state.script.groups[idGroup].loopRange}" }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 0, -1, getRegexFilter(numRegex, "[.]+"),
                        { dialog, which ->
                            val default = ""
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val group = mainVM.state.script.groups[idGroup]
                        if (input.isEmpty()) {
                            mainVM.setGroup(idGroup, group.copy(loopRange = (1..1)))
                        } else if (!input.contains("..")) {
                            dialog.view.shakeReverse()
                            view.context.showXToastError("输入格式错误")
                            return@showRightTextInputDialog
                        } else {
                            val range = input.split("..")
                            if (range.isEmpty() || range.size != 2) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误")
                                return@showRightTextInputDialog
                            }
                            val start = range[0].toIntOrNull()
                            val end = range[1].toIntOrNull()
                            if (start == null || end == null) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误:数字转换错误")
                                return@showRightTextInputDialog
                            }
                            if (start > end) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误,起始($start)>结束($end)")
                                return@showRightTextInputDialog
                            }
                            mainVM.setGroup(idGroup, group.copy(loopRange = (start..end)))
                        }
                        dialog.dismiss()
                    }
                }
            }
        }
    }
}